<?php
/**
 * Created by PhpStorm.
 * User: KHACHUNG
 * Date: 3/24/14
 * Time: 6:37 PM
 */
class UsersController extends AppController{
    var $name='Users';
    function login(){
        $this->layout='login';
        if($this->request->is('post')){
            if($this->Auth->login()){
                $this->redirect($this->Auth->redirect());
            }
            else {
                $this->Session->setFlash('Tài Khoản / Mật Khẩu Sai!');
            }
        }
    }
    function logout(){
        $this->redirect($this->Auth->logout());
    }
    function listuser(){
        $this->set('listuser',$this->User->find('all'));
    }
    function capnhat(){
        //Them Script cho trang
        if($this->data){
            $data=$this->data;
            $img=$this->Upload->upload($data['User']['img']);
            if($img){
                $data['User']['img']=$img;
                $data['User']['status']=1;
                $data['User']['password']=$this->Auth->password($data['User']['password']);
                $this->User->save($data);
                $this->Session->setFlash('Cập Nhật Thành Công!!');
                $this->redirect('');
            }

        }
        $this->set('listuser',$this->User->find('all'));

    }
    function add_edit($id=0){
        $this->set('chitiet',$this->User->find('all',array('conditions'=>array('id'=>$id))));
        if($this->RequestHandler->isAjax()){
            $this->render('frm_save','ajax');
        }
    }
    function delete($id){
        if($this->RequestHandler->isAjax()){
            $this->User->delete($id);
            $this->render('success','ajax');
        }
    }
    function chitiet(){
        $this->set('nguoidung',$this->User->find('all',array('conditions'=>array('id'=>$this->request->params['id']))));
    }
}